/*
 * Copyright (C) 2011 Patrik �kerfeldt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.CenterlunboActivity;
import cn.otrue.patienthealthmanager.bean.MessageNewsModel;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;



public class ImageAdapter extends BaseAdapter {
	private List<MessageNewsModel> goods;
	private Context mContext;
	private LayoutInflater mInflater;
	public static boolean isDown = false;
//	private static final int[] ids = {R.drawable.test1, R.drawable.test2, R.drawable.test3 };

	
	
	
		public void ImageSet(String imgurl, ImageView img_headPhoto) {

			DisplayImageOptions options = new DisplayImageOptions.Builder()
					//.showImageOnLoading(R.drawable.iv_user_photo)
					//.showImageOnFail(R.drawable.iv_user_photo)
					//.showImageForEmptyUri(R.drawable.iv_user_photo)
					.cacheInMemory(true).cacheOnDisk(true)
					.bitmapConfig(Bitmap.Config.RGB_565).build();
			ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
		}

	public ImageAdapter(List<MessageNewsModel> goods,Context context) {
		mContext = context;
		this.goods = goods;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;   //返回很大的值使得getView中的position不断增大来实现循环
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.image_item, null);
		
		}ImageView imgView = (ImageView) convertView.findViewById(R.id.imgView);
		MessageNewsModel eachMap = goods.get(position%goods.size());
		String url = eachMap.getImagepath();
		ImageSet(url, imgView);
//		convertView.setOnTouchListener(new OnTouchListener() {
//			private Long btime = 0L;
//			private Long etime = 0L;
//
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				if (event.getAction() == MotionEvent.ACTION_DOWN) {
//					btime = System.currentTimeMillis();
//					isDown = true;
//				} else if (event.getAction() == MotionEvent.ACTION_UP) {
//					etime = System.currentTimeMillis();
//					isDown = false;
//					if ((etime - btime) <= 500) {
//						Intent intent = new Intent(mContext,
//								CenterlunboActivity.class);
//						intent.putExtra("Description", goods.get(position%goods.size())
//								.getDescription());
//						mContext.startActivity(intent);
//					}
//				}
//				return true;
//
//			}
//		});
		
		
		
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,CenterlunboActivity.class);
				intent.putExtra("Description", goods.get(position%goods.size())
						.getDescription());
				
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

}
